package daoIpmt;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import beans.Member;
import dao.UserDao;
import utils.DataSourceUtils;

public class UserDaoIpmt implements UserDao {
//	 `id` int(6) NOT NULL AUTO_INCREMENT,
//	  `username` varchar(20) NOT NULL UNIQUE,
//	  `password` varchar(20) NOT NULL ,
//	  `nikename` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
//	  `gendar` VARCHAR(1) DEFAULT NULL,
//	  `location` varchar(20) DEFAULT NULL,
//	  `birthday` date DEFAULT NULL,
//	  PRIMARY KEY (`id`)
	private  DataSource ds=DataSourceUtils.getDataSource();
	private QueryRunner qr=new QueryRunner(ds);
	@Override
	public void addUser(Member member) {
		String sql="insert into member values(null,?,?,?,?,?,?,?,?,?,?)";
		try {
			qr.update(sql,member.getUsername(),member.getPassword(),member.getNikename(),
					member.getGendar(),member.getLocation(),member.getBirthday(),member.getHeight(),member.getIncome(),member.getStudy(),member.getWedding());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public Member getMemberByUsername(String username) {
		String sql="select * from member where username=?";
		try {
			Member member=qr.query(sql, new BeanHandler<Member>(Member.class),username);
			return member;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Member getUserByUsernameAndPwd(String username, String password) {
		String sql = "select * from member where username = ? and password = ?";
		try {
			Member member = qr.query(sql, new BeanHandler<Member>(Member.class), username, password);
			return member;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public Member getUserById(int id) {
		String sql = "select * from member where id=?";
		try {
			Member member = qr.query(sql, new BeanHandler<Member>(Member.class),id);
			return member;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public void inheat(Member member) {
		String sql="update personinformation set heat=heat+1 where id=?";
		try {
			qr.update(sql,member.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void deheat(Member member) {
		String sql="update personinformation set heat=heat-1 where id=?";
		try {
			qr.update(sql,member.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
